/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.eagleeyes.hoverbuttons.jogl;

import java.awt.Component;
import javax.media.opengl.GL;

/**
 * a GLEEButton whose position is relative - a percentage of the total area.  To use this kind of button,
 * glOrtho should be set up as the component size.
 * @author Jesse Bahr
 */
public class GLEERelativeButton extends GLEEButton{
  /**
   * the percent of the screen to use for a buffer between the button and the start of the highlight
   */
  private static final float BUFFER_LEN=.01f;
  private float x, y, width, height;
  
  /**
   * the parent component is maintained for reference, so we know when a contains request comes in, what
   * height and width it has.
   */
  private Component parentComp;
  
  /**
   * Set up the button in current screen coordinates
   * @param parentComp
   * @param x
   * @param y
   * @param width
   * @param height
   */
  public GLEERelativeButton(Component parentComp, int x, int y, int width, int height){
    super();
    this.parentComp = parentComp;
    setX(x);
    setY(y);
    setWidth(width);
    setHeight(height);
  }
  
  /**
   * set up in relative screen coordinates.  all arguments should be between 0 and 1, with 1 being
   * the size of the screen at any given time.
   * @param parentComp
   * @param x
   * @param y
   * @param width
   * @param height
   */
  public GLEERelativeButton(Component parentComp, float x, float y, float width, float height){
    super();
    this.parentComp = parentComp;
    setX(x);
    setY(y);
    setWidth(width);
    setHeight(height);
  }
  
  @Override public void setX(int x){
    if (parentComp != null)
      this.x = ((float) x) / ((float) parentComp.getWidth());
  }
  
  @Override public int getX(){
    return (int) (((float) parentComp.getWidth()) * x);
  }
  
  public void setX(float x){
    this.x = x;
  }
  
  public float getFloatX(){
    return x;
  }
  
  @Override public void setY(int y){
    if (parentComp != null)
      this.y = ((float) y) / ((float) parentComp.getHeight());
  }
  
  @Override public int getY(){
    return (int) (((float) parentComp.getHeight()) * y);
  }
  
  public void setY(float y){
    this.y = y;
  }
  
  public float getFloatY(){
    return y;
  }
  
  @Override public void setWidth(int width){
    if (parentComp != null)
      this.width = ((float) width) / ((float) parentComp.getWidth());
  }
  
  @Override public int getWidth(){
    return (int) (((float) parentComp.getWidth()) * width);
  }
  
  public void setWidth(float width){
    this.width = width;
  }
  
  public float getFloatWidth(){
    return width;
  }
  
  @Override public void setHeight(int height){
    if (parentComp != null)
      this.height = ((float) height) / ((float) parentComp.getHeight());
  }
  
  @Override public int getHeight(){
    return (int) (((float) parentComp.getHeight()) * height);
  }
  
  public void setHeight(float height){
    this.height = height;
  }
  
  public float getFloatHeight(){
    return height;
  }
  
  
  
  
  
  @Override public boolean contains(int x, int y){
    float floatwid = parentComp.getWidth();
    float floatht = parentComp.getHeight();
    int ix = (int) (this.x * floatwid), 
        iy = (int) (this.y * floatht), 
        iwid = (int) (width * floatwid), 
        iht = (int) (height * floatht);
    
    
    return (x >= ix && x - ix < iwid &&
        y >= iy && y - iy < iht);
  }

  @Override public void draw(GL gl){
    float percent = getPercent();
    //percent < 0 indicates that it's not hovered currently.
    if (percent >= 0f){
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glPushMatrix();
      gl.glScalef(parentComp.getWidth(), parentComp.getHeight(), 1f);

      float transx = x + width / 2;
      float transy = y + height / 2;
      gl.glTranslatef(transx, transy, 0f);

      float scalex = width - BUFFER_LEN - BUFFER_LEN;
      float scaley = height - BUFFER_LEN - BUFFER_LEN;
      gl.glScalef(scalex, scaley, 1f);
      
      float oneMinusPerc = 1f - (percent * percent);
      gl.glScalef(oneMinusPerc, oneMinusPerc, oneMinusPerc);
      gl.glColor4fv(getColor(), 0);
      
      //lower-left
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, -.5f, 0f);
        gl.glVertex3f(-.2f, -.5f, 0f);
        gl.glVertex3f(-.2f, -.4f, 0f);
        gl.glVertex3f(-.5f, -.4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, -.4f, 0f);
        gl.glVertex3f(-.4f, -.4f, 0f);
        gl.glVertex3f(-.4f, -.2f, 0f);
        gl.glVertex3f(-.5f, -.2f, 0f);
      gl.glEnd();

      //upper-left
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, .5f, 0f);
        gl.glVertex3f(-.2f, .5f, 0f);
        gl.glVertex3f(-.2f, .4f, 0f);
        gl.glVertex3f(-.5f, .4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, .4f, 0f);
        gl.glVertex3f(-.4f, .4f, 0f);
        gl.glVertex3f(-.4f, .2f, 0f);
        gl.glVertex3f(-.5f, .2f, 0f);
      gl.glEnd();
      

      //lower-right
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, -.5f, 0f);
        gl.glVertex3f(.2f, -.5f, 0f);
        gl.glVertex3f(.2f, -.4f, 0f);
        gl.glVertex3f(.5f, -.4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, -.4f, 0f);
        gl.glVertex3f(.4f, -.4f, 0f);
        gl.glVertex3f(.4f, -.2f, 0f);
        gl.glVertex3f(.5f, -.2f, 0f);
      gl.glEnd();

      //upper-right
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, .5f, 0f);
        gl.glVertex3f(.2f, .5f, 0f);
        gl.glVertex3f(.2f, .4f, 0f);
        gl.glVertex3f(.5f, .4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, .4f, 0f);
        gl.glVertex3f(.4f, .4f, 0f);
        gl.glVertex3f(.4f, .2f, 0f);
        gl.glVertex3f(.5f, .2f, 0f);
      gl.glEnd();
      
      
      gl.glPopMatrix();
    }
  }
}